package org.example.springbootpra1.demos.bean.enums.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;import org.example.springbootpra1.demos.bean.enums.DeviceStatus;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeviceStatusTypeHandler extends BaseTypeHandler<DeviceStatus> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, DeviceStatus parameter, JdbcType jdbcType) throws SQLException {
        try {
            ps.setInt(i, parameter.getCode());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public DeviceStatus getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return rs.wasNull() ? null : DeviceStatus.fromCode(code);
    }

    @Override
    public DeviceStatus getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int code = rs.getInt(columnIndex);
        return rs.wasNull() ? null : DeviceStatus.fromCode(code);
    }

    @Override
    public DeviceStatus getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int code = cs.getInt(columnIndex);
        return cs.wasNull() ? null : DeviceStatus.fromCode(code);
    }
}
